package gui;


import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

import Shared.AmbType;
import Shared.Map;
import Shared.MapPosition;
import Simulator.RealAmbulance;
import Simulator.EmergencyGenerator;
import Simulator.EnvironmentDatabase;
import Simulator.MDT;
import Simulator.AmbulanceGenerator;
import Simulator.MdtGUI.MDTScreen;

public class MapGUIListener implements MouseListener {
	
	private EmergencyGenerator erGen;
	private AmbulanceGenerator ambGen;
	private EnvironmentDatabase db;
	private MapGUI mapgui;
	
	public MapGUIListener (MapGUI mapgui) {
		this.ambGen = AmbulanceGenerator.getInstance();
		this.erGen = EmergencyGenerator.getInstance();
		this.db = EnvironmentDatabase.getInstance();
		this.mapgui = mapgui;
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		Point p = arg0.getPoint();

		for (MapPosition hospital : this.db.getHospitals()) {
			Point corrHosp = new Point();
			corrHosp.setLocation(hospital.getPoint().getX(), hospital.getPoint().getY());

			if (p.distance(corrHosp) < 20) {

				Object[] possibilities = { "Simple", "Equiped" };

				String s = (String) JOptionPane.showInputDialog(this.mapgui, "Ambulance Type :", "Create a new ambulance",
						JOptionPane.QUESTION_MESSAGE, null, possibilities, "Simple");

				if ((s != null) && (s.length() > 0)) {
					if (s.equals("Equiped")) {
						this.ambGen.createAmbulance(Map.getInstance().getNearestPos(hospital), AmbType.EQUIPED);
					} else {
						this.ambGen.createAmbulance(Map.getInstance().getNearestPos(hospital), AmbType.SIMPLE);
					}
				}
				this.mapgui.repaint();
				return;
			}
		}

		for (RealAmbulance realAmb : db.getAllAmbulances()) {
			int x = (int) realAmb.getPosition().getPoint().getX();
			int y = (int) realAmb.getPosition().getPoint().getY();
			double dist = p.distance(new Point(x, y));
			if (dist < 20.0) {
				MDT tempMdt = realAmb.getMdt();
				if (tempMdt.isGUIDisplayed()) {
					tempMdt.focusOnGUI();
				} else {
					MDTScreen gui = new MDTScreen(realAmb.getMdt());
					realAmb.getMdt().setGUI(gui);
				}

				return;
			}
		}

		/*
		 * not a clickable point ? => enqueue it as beeing the new er to
		 * generate
		 */
		// disp("Add this ER");

		Object[] possibilities = { "Normal", "Urgent" };

		String s = (String) JOptionPane.showInputDialog(this.mapgui, "Emergency Type :", "Create a new emergency",
				JOptionPane.QUESTION_MESSAGE, null, possibilities, "Normal");

		if ((s != null) && (s.length() > 0)) {
			if (s.equals("Urgent"))
				this.erGen.addERFromPosition(new MapPosition(p), true);
			else
				this.erGen.addERFromPosition(new MapPosition(p), false);
		}

		this.mapgui.repaint();

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

}